<?php

namespace app\model\v1\recovery;

use app\model\system\admin\SystemAdmin;
use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;


class SystemOperationLog extends Model
{

   

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 日志记录
     * @param $param
     * @return string
     */
    public static function addSystemLog($admin_id,$module,$page,$act,$type,$param,$ip,$desc)
    {
        $admin_name = Db::name("system_admin")->where("id", $admin_id)->value("account") ?? "";

        $addLog = [
            "admin_id"      => $admin_id, //操作员id
            "admin_name"    => $admin_name, //操作员名称
            "module"        => $module, //模块
            "page"          => $page, //页面
            "act"           => $act, //指令
            "type"          => $type, //类型
            "param"         => json_encode($param), //参数
            "ip"            => $ip, //登录IP
            "desc"          => "登录账号-{$admin_name}".$desc, //简介
            "create_time"   => time(),
        ];

        return self::create($addLog);
    }


    /**
     * 日志管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSystemLogList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
              case when module='recovery' then '闲置转让' 
                when module='giftcard' then '礼品卡' 
                when module='recruit' then '求职招聘' 
                when module='repair' then '快修' 
                when module='water' then '水站' 
                when module='mark' then '超市' 
                end as module_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据关键字搜索
                if(!empty($param["word"])) $query->where("desc",'like', '%'.$param["word"].'%');
                //根据模块搜索
                if(!empty($param["module"])) $query->where("module", $param["module"]);
                //根据类型搜索
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据关键字搜索
                if(!empty($param["word"])) $query->where("desc",'like', '%'.$param["word"].'%');
                //根据模块搜索
                if(!empty($param["module"])) $query->where("module", $param["module"]);
                //根据类型搜索
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->count();

        return $data;
    }



}